<?php


namespace App\Tools;


use App\Models\MerchantShop;
use App\Models\MerchantUser;
use App\Models\PushTask;
use App\Models\ShopGroup;
use App\Models\SystemConfig;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Schema;

/**
 * 复购查询
 * Class FindRepeats
 * @package App\Tools
 */
class FindRepeats
{
    private $tasks;

    public function __construct($tasks)
    {
        $this->tasks = $tasks;
    }

    public function getNotExists()
    {
        $isTbOrJd = $this->tasks['platform_type'] == 1 || $this->tasks['platform_type'] == 2 || $this->tasks['platform_type'] == 3 || $this->tasks['platform_type'] == 4;

        // 店铺组限制
        $shopGroupSql = "";
        if (SystemConfig::findValue('dispatch.posgrouprepeatlimit') == 1) {
            // 查询组ID
            $group_id = MerchantShop::where('id', $this->tasks['shop_id'])->value('group_id');
            if (isset($group_id) && $group_id > 0) {
                $shopGroupSql = $this->shopGroupSql($group_id);
            }
        }
        // 店铺限制,优先店铺组
        $shopSql = "";
        if (SystemConfig::findValue('dispatch.shoprepeatlimit') == 1 && empty($shopGroupSql)) {
            $shopSql = $this->shopSql($isTbOrJd);
        }
        // 商品组限制
        $goodsSql = "";
        if (SystemConfig::findValue('dispatch.goodrepeatlimit') == 1) {
            $goodsSql = $this->goodsSql($isTbOrJd);
        }
        // 派单同商户限制是否开启
        $isMerchantRepeatLimit = MerchantUser::where('id', $this->tasks['merchant_id'])->value('dispatch_limit_status');
        // 同商户限制
        $merchantSql = "";
        if (SystemConfig::findValue('dispatch.posrepeatlimit') == 1 && $isMerchantRepeatLimit) {
            $merchantSql = $this->merchantSql($isTbOrJd);
        }
        // 组装sql
        $subSql = " UNION ";
        if (!empty($shopGroupSql)) {
            $subSql .= $shopGroupSql." UNION ";
        }
        if (!empty($shopSql)) {
            $subSql .= $shopSql. " UNION ";
        }
        if (!empty($goodsSql)) {
            $subSql .= $goodsSql." UNION ";
        }
        if (!empty($merchantSql)) {
            $subSql .= $merchantSql;
        }
        $subSql = trim(trim($subSql), "UNION");
        return $subSql;
    }

    /**
     * 返回店铺组复购查询sql
     * @param $group_id
     * @return int|string
     */
    private function shopGroupSql($group_id)
    {
        $shopIds = MerchantShop::where('group_id', $group_id)->pluck('id');
        $shopIdsStr = "";
        if (!empty($shopIds)) {
            foreach ($shopIds as $value) {
                $shopIdsStr .= $value.",";
            }
            $shopIdsStr = rtrim($shopIdsStr, ",");
        }
        $sql = "select push_id from xx_push_task where";
        if (!empty($shopIdsStr)) {
            $sql .= " shop_id in ({$shopIdsStr})";
            $days = ShopGroup::where('id', $group_id)->value('days');
            if ($days) {
                $st = time() - ($days * 24 * 60 * 60);
                $et = time();
                $sql .= " and create_time between {$st} and {$et}";
            }
            return $sql;
        }
        return false;
    }

    /**
     * 返回店铺复购查询sql
     * @param $isTbOrJd
     * @return string
     */
    private function shopSql($isTbOrJd)
    {
        $sql = "select push_id from xx_push_task where shop_id = {$this->tasks['shop_id']} and task_type = 1 and status in (6, 7, 8)";
        // 同商铺限制
        $days = SystemConfig::findValue($isTbOrJd ? 'posputplan.shop1' : 'posputplan.shop2');
        if ($days) {
            $st = time() - ($days * 24 * 60 * 60);
            $et = time();
            $sql .= " and create_time between {$st} and {$et}";
        }
        return $sql;
    }

    /**
     * 返回商户复购查询sql
     * @param $isTbOrJd
     * @return string
     */
    private function merchantSql($isTbOrJd)
    {
        $sql = "select push_id from xx_push_task where merchant_id = {$this->tasks['merchant_id']} and task_type = 1 and status in (6, 7, 8)";
        if (in_array($this->tasks['platform_type'], [1, 2, 4])) {
            $sql .= " and shop_type in (1, 2, 4)";
        }else{
            $sql .= " and shop_type = {$this->tasks['platform_type']}";
        }
        // 同商品限制
        $days = SystemConfig::findValue($isTbOrJd ? 'posputplan.pos1' : 'posputplan.pos2');
        if ($days) {
            $st = time() - (60 * 24 * 60 * 60);
            $et = time();
            $sql .= " and create_time between {$st} and {$et}";
        }
        return $sql;
    }

    /**
     * 返回商品复购查询sql
     * @param $isTbOrJd
     * @return string
     */
    private function goodsSql($isTbOrJd)
    {
        $sql = "select push_id from xx_push_task where goods_id = {$this->tasks['goods_id']} and task_type = 1";
        // 是否派给领取后放弃的用户
        if (SystemConfig::findValue('dispatch.togiveupuser', 1) == 1) {
            $sql .= " and status in (6, 7, 8)";
        }else{
            $sql .= " and status in (7, 6, 8, 3)";
        }
        // 同商品限制天数
        $limitDays = SystemConfig::findValue($isTbOrJd ? 'posputplan.good1' : 'posputplan.good2');
        if ($limitDays) {
            $st = time() - (60 * 24 * 60 * 60);
            $et = time();
            $sql .= " and create_time between {$st} and {$et}";
        }
        return $sql;
    }
}
